import req from "@/uni-sdk/request/request.js";
import {
	encode
} from "@/uni-sdk/lib/base64.js";
// import FormData from "@/uni-sdk/lib/formData.js";

const Api = {
	login: "zyx-auth/oauth2/token",
	bind: "mall-ums/app-api/v1/members/mobile/bind",
	communityList: "mall-ums/app-api/v1/communities/list",
	me: "mall-ums/app-api/v1/members/me",
	member: "mall-ums/app-api/v1/members/member",
};

export function loginInter(data) {
	// const formData = new FormData();
	// formData.append("grant_type", "wx_mini_app");
	// formData.append("code", data.code);
	// data.grant_type = "wx_mini_app";
	const formData = `code=${data.code}&grant_type=wx_mini_app`;
	const username = "mall-app";
	const password = "123456";
	const userMsg = username + ":" + password;
	const basicAuth = `Basic ${encode(userMsg)}`;
	
	return req.post({
		url: Api.login,
		// data: reqData.buffer,
		data: formData,
		header: {
			'Authorization': basicAuth,
		}
	}, {
		withToken: false,
		useGlobalLoading: true,
		contentTypeKey: "FORM_URLENCODED",
		// contentTypeKey: "FORM_DATA",
	});
}

export function bindPhone(data) {
	return req.post({
		url: Api.bind,
		data,
	}, {
		useGlobalLoading: true,
	});
}

export function getCommunityList(){
	return req.commonGet(Api.communityList, true);
}

export function getMemberInfo(options){
	return req.commonGet(Api.me, {}, options);
}

export function updateMember(data){
	return req.put({
		url: Api.member,
		data,
	},{
		useGlobalLoading: true,
	});
}